package edu.bbu.commonproject.desktop.gui.controller.concrete;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;

import edu.bbu.commonproject.desktop.gui.view.BaseFrameInterface;
import edu.bbu.commonproject.desktop.gui.view.FileListInterface;
import edu.bbu.commonproject.desktop.gui.view.LoginInterface;
import edu.bbu.commonproject.desktop.gui.view.SetupInterface;
import edu.bbu.commonproject.desktop.gui.view.swing.base.BaseFrame;
import edu.bbu.commonproject.desktop.gui.view.swing.utils.Tray;

/**
 * The base controller class.
 */
public class BaseFrameController
{	
	/** The log on controller. */
	private LogonController logonController;
	
	/** The setup controller. */
	private SetupController setupController;
	
	/** The file list controller. */
	private FileListController fileListController;
	
	/** The base frame interface. */
	private BaseFrameInterface baseFrameInterface;
	
	
	/**
	 * The constructor.
	 * @param baseFrameInterface the base frame interface
	 */
	public BaseFrameController(BaseFrameInterface baseFrameInterface)
	{
		this.baseFrameInterface = baseFrameInterface;
		
		logonController = new LogonController(this, (LoginInterface) baseFrameInterface.getLogonView());
		setupController = new SetupController(this, (SetupInterface) baseFrameInterface.getSetupView());
		fileListController = new FileListController(this, (FileListInterface) baseFrameInterface.getFileView());
	
		setListeners();
	}
	
	/**
	 * Setting the listeners.
	 */
	private void setListeners()
	{
		((JFrame) baseFrameInterface.getBaseFrame()).addWindowListener(new WindowAdapter()
		{
			public void windowIconified(WindowEvent e)
			{				
				if(((Tray) baseFrameInterface.getTrayIcon()).isTraySupprted())
				{
					((JFrame) baseFrameInterface.getBaseFrame()).setVisible(false);
				}
			}
		});
		
		((Tray) baseFrameInterface.getTrayIcon()).addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				bringBaseFrameToFront();
			}
		});		
	}
	
	/**
	 * Getting the base frame.
	 * @return the base frame.
	 */
	public BaseFrame getBaseFrame()
	{
		return (BaseFrame) baseFrameInterface.getBaseFrame();
	}
	
	/**
	 * Bring the base frame to front.
	 */
	public void bringBaseFrameToFront()
	{
		((JFrame) baseFrameInterface.getBaseFrame()).setVisible(true);
		((JFrame) baseFrameInterface.getBaseFrame()).setExtendedState(JFrame.NORMAL);
	}
	
	/**
	 * Getting the file list controller.
	 * @return the file list controller.
	 */
	public FileListController getFileListController()
	{
		return fileListController;
	}
	
	/**
	 * Getting the log on controller.
	 * @return the log on controller.
	 */
	public LogonController getLogonController()
	{
		return logonController;
	}
	
	/**
	 * Getting the setup controller.
	 * @return the setup controller.
	 */
	public SetupController getSetupController()
	{
		return setupController;
	}
}
